home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / GENCTXT.ZIP / CHAP5.TXT < prev    next >
Text File  |  1987-11-21  |  34KB  |  785 lines

  1.  
  2.             Chapter 5 - Functions, variables, and prototypes
  3.  
  4.  
  5.                       OUR FIRST USER DEFINED FUNCTION
  6.  
  7.              Load  and examine the file SUMSQRES.C for an example of
  8.         a C program with functions.   Actually this is not the first
  9.         function  we have encountered because the "main" program  we
  10.         have been using all along is technically a function,  as  is
  11.         the  "printf" function.   The "printf" function is a library
  12.         function that was supplied with your compiler.
  13.  
  14.              Notice the executable part of this program which begins
  15.         in  line  8.   It  begins  with  a  line  that  simply  says
  16.         "header()",  which  is the way to call  any  function.   The
  17.         parentheses are required because the C compiler uses them to
  18.         determine  that  it  is a function call  and  not  simply  a
  19.         misplaced variable.  When the program comes to this line  of
  20.         code, the function named "header" is called, its  statements
  21.         are executed, and control returns to the statement following
  22.         this call.  Continuing on we come to a "for" loop which will
  23.         be  executed 7 times and which calls another function  named
  24.         "square" each time through the loop, and finally a  function
  25.         named "ending" will be called and executed.  For the  moment
  26.         ignore  the  "index"  in  the parentheses  of  the  call  to
  27.         "square".  We have seen that this program therefore calls  a
  28.         header, 7 square calls, and an ending. Now we need to define
  29.         the functions.
  30.  
  31.                            DEFINING THE FUNCTIONS
  32.  
  33.              Following the main program you will see another program
  34.         that  follows all of the rules set forth so far for a "main"
  35.         program  except that it is named "header()".   This  is  the
  36.         function which is called from within the main program.  Each
  37.         of  these  statements are executed,  and when they  are  all
  38.         complete, control returns to the main program.
  39.  
  40.              The  first  statement sets the variable "sum" equal  to
  41.         zero because we will use it to accumulate a sum of  squares.
  42.         Since  the  variable  "sum" is defined as  an  integer  type
  43.         variable  prior to the main program,  it is available to  be
  44.         used  in  any of the following functions.   It is  called  a
  45.         "global" variable,  and it's scope is the entire program and
  46.         all  functions.   More  will  be  said about  the  scope  of
  47.         variables near the end of this chapter.  The next  statement
  48.         outputs  a header message to the monitor.   Program  control
  49.         then  returns  to  the  main  program  since  there  are  no
  50.         additional statements to execute in this function.
  51.  
  52.              It should be clear to you that the two executable lines
  53.         from  this  function  could be moved to  the  main  program,
  54.         replacing the header call,  and the program would do exactly
  55.         the same thing that it does as it is now written.  This does
  56.  
  57.  
  58.                                   Page 29
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.             Chapter 5 - Functions, variables, and prototypes
  69.  
  70.  
  71.         not minimize the value of functions,  it merely  illustrates
  72.         the operation of this simple function in a simple way.   You
  73.         will find functions to be very valuable in C programming.
  74.  
  75.                        PASSING A VALUE TO A FUNCTION
  76.  
  77.              Going  back  to the main program,  and the  "for"  loop
  78.         specifically,  we find the new construct from the end of the
  79.         last lesson used in the last part of the "for" loop,  namely
  80.         the  "index++".  You should get used to seeing this, as  you
  81.         will see it a lot in C programs.
  82.  
  83.              In the call to the function "square",  we have an added
  84.         feature, namely the variable "index" within the parentheses.
  85.         This  is  an indication to the compiler that when you go  to
  86.         the function,  you wish to take along the value of index  to
  87.         use in the execution of that function.  Looking ahead at the
  88.         function  "square",  we  find that another variable name  is
  89.         enclosed in its parentheses,  namely the variable  "number".
  90.         This  is  the name we prefer to call the variable passed  to
  91.         the  function when we are in the function.   We can call  it
  92.         anything  we wish as long as it follows the rules of  naming
  93.         an identifier.   Since the function must know what type  the
  94.         variable  is,  it is defined following the function name but
  95.         before the opening brace of the function itself.   Thus, the
  96.         line  containing "int number;" tells the function  that  the
  97.         value  passed to it will be an integer type variable.   With
  98.         all of that out of the way,  we now have the value of  index
  99.         from  the main program passed to the function "square",  but
  100.         renamed "number", and available for use within the function.
  101.         This  is the "classic" style of defining function  variables
  102.         and has been in use since C was originally defined.  A newer
  103.         method is gaining in popularity due to its many benefits and
  104.         will be discussed later in this chapter.
  105.  
  106.              Following the opening brace of the function,  we define
  107.         another  variable "numsq" for use only within  the  function
  108.         itself,  (more  about  that  later)  and  proceed  with  the
  109.         required  calculations.   We set "numsq" equal to the square
  110.         of  number,  then add numsq to the current total  stored  in
  111.         "sum".   Remember  that "sum += numsq" is the same as "sum =
  112.         sum + numsq" from the last lesson.   We print the number and
  113.         its square, and return to the main program.
  114.  
  115.                   MORE ABOUT PASSING A VALUE TO A FUNCTION
  116.  
  117.              When we passed the value of "index" to the function,  a
  118.         little  more  happened  than meets  the  eye.   We  did  not
  119.         actually  pass  the  value  of index  to  the  function,  we
  120.         actually  passed  a  copy of the value.   In  this  way  the
  121.         original value is protected from accidental corruption by  a
  122.  
  123.  
  124.                                   Page 30
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.             Chapter 5 - Functions, variables, and prototypes
  135.  
  136.  
  137.         called  function.   We  could  have  modified  the  variable
  138.         "number" in any way we wished in the function "square",  and
  139.         when we returned to the main program, "index" would not have
  140.         been  modified.   We thus protect the value of a variable in
  141.         the main program from being accidentally corrupted,  but  we
  142.         cannot  return  a value to the main program from a  function
  143.         using this technique.  We will find a well defined method of
  144.         returning  values  to  the main program or  to  any  calling
  145.         function  when we get to arrays and another method  when  we
  146.         get  to pointers.   Until then the only way you will be able
  147.         to  communicate  back to the calling function will  be  with
  148.         global  variables.    We  have  already  hinted  at   global
  149.         variables  above,  and will discuss them in detail later  in
  150.         this chapter.
  151.  
  152.              Continuing  in  the main program,  we come to the  last
  153.         function call, the call to "ending".  This call simply calls
  154.         the last function which has no local variables defined.   It
  155.         prints out a message with the value of "sum" contained in it
  156.         to  end the program.   The program ends by returning to  the
  157.         main  program and finding nothing else to do.   Compile  and
  158.         run this program and observe the output.
  159.  
  160.                         NOW TO CONFESS A LITTLE LIE
  161.  
  162.              I told you a short time ago that the only way to get  a
  163.         value  back to the main program was through use of a  global
  164.         variable,  but  there  is another way which we will  discuss
  165.         after  you load and display the file  named  SQUARES.C.   In
  166.         this  file we will see that it is simple to return a  single
  167.         value  from a called function to the calling function.   But
  168.         once again,  it is true that to return more than one  value,
  169.         we will need to study either arrays or pointers.
  170.  
  171.              In the main program, we define two integers and begin a
  172.         "for"  loop  which  will be executed  8  times.   The  first
  173.         statement of the "for" loop is "y = squ(x);", which is a new
  174.         and rather strange looking construct.  From past experience,
  175.         we  should have no trouble understanding that  the  "squ(x)"
  176.         portion  of  the statement is a call to the  "squ"  function
  177.         taking along the value of "x" as a variable.  Looking  ahead
  178.         to the function itself we find that the function prefers  to
  179.         call  the variable "in" and it proceeds to square the  value
  180.         of  "in" and call the result "square".  Finally, a new  kind
  181.         of  a statement appears, the "return" statement.  The  value
  182.         within  the parentheses is assigned to the  function  itself
  183.         and  is  returned  as a usable value in  the  main  program.
  184.         Thus,  the function call "squ(x)" is assigned the  value  of
  185.         the square and returned to the main program such that "y" is
  186.         then  set  equal  to  that value.   If  "x"  were  therefore
  187.  
  188.  
  189.  
  190.                                   Page 31
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.             Chapter 5 - Functions, variables, and prototypes
  201.  
  202.  
  203.         assigned  the value 4 prior to this call, "y" would then  be
  204.         set to 16 as a result of this line of code.
  205.  
  206.              Another  way  to  think  of this  is  to  consider  the
  207.         grouping  of characters "squ(x)" as another variable with  a
  208.         value  that is the square of "x",  and this new variable can
  209.         be used any place it is legal to use a variable of its type.
  210.         The values of "x" and "y" are then printed out.
  211.  
  212.              To  illustrate  that the grouping of  "squ(x)"  can  be
  213.         thought  of as just another variable,  another "for" loop is
  214.         introduced in which the function call is placed in the print
  215.         statement rather than assigning it to a new variable.
  216.  
  217.              One  last  point must be made,  the  type  of  variable
  218.         returned must be defined in order to make sense of the data,
  219.         but the compiler will default the type to integer if none is
  220.         specified.   If  any  other  type is  desired,  it  must  be
  221.         explicitly defined.   How to do this will be demonstrated in
  222.         the next example program.
  223.  
  224.              Compile  and  run  this program  which  also  uses  the
  225.         "classic" method of defining function variables.
  226.  
  227.                             FLOATING POINT FUNCTIONS
  228.  
  229.              Load the program FLOATSQ.C for an example of a function
  230.         with a floating point type of return.  It begins by defining
  231.         a global floating point variable we will use later.  Then in
  232.         the  "main"  part  of the program,  an integer  is  defined,
  233.         followed  by two floating point variables,  and then by  two
  234.         strange  looking definitions.   The expressions "sqr()"  and
  235.         "glsqr()"  look like function calls and they are.   This  is
  236.         the proper way in C to define that a function will return  a
  237.         value that is not of the type "int", but of some other type,
  238.         in  this case "float".   This tells the compiler that when a
  239.         value  is returned from either of these  two  functions,  it
  240.         will be of type "float".  This is, once again, the "classic"
  241.         method of defining functions.
  242.  
  243.              Now  refer to the function "sqr" near the center of the
  244.         listing and you will see that the function name is  preceded
  245.         by the name "float".   This is an indication to the compiler
  246.         that  this  function will return a value of type "float"  to
  247.         any program that calls it.   The function is now  compatible
  248.         with  the call to it.   The line following the function name
  249.         contains  "float inval;",  which indicates to  the  compiler
  250.         that  the variable passed to this function from the  calling
  251.         program will be of type "float".
  252.  
  253.  
  254.  
  255.  
  256.                                   Page 32
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.             Chapter 5 - Functions, variables, and prototypes
  267.  
  268.  
  269.              The next function,  namely "glsqr",  will also return a
  270.         "float"  type  variable,  but it uses a global variable  for
  271.         input.   It  also does the squaring right within the  return
  272.         statement  and  therefore has no need to define  a  separate
  273.         variable to store the product.
  274.  
  275.              The  overall structure of this program should  pose  no
  276.         problem and will not be discussed in any further detail.  As
  277.         is customary with all example programs, compile and run this
  278.         program.
  279.  
  280.                THERE IS A BUG IN THE FIRST VERSION OF TURBO C
  281.  
  282.              When you run this program, if you are using version 1.0
  283.         of  Turbo C, you will find that the function  named  "sqr()"
  284.         will return a value of zero.  This is because it receives  a
  285.         zero  from  the  calling program.  The  program  is  written
  286.         correctly  but  the  compiler has a bug in it.   A  call  to
  287.         Borland  resulted  in  a fix by simply  using  the  "modern"
  288.         method  of  function definition rather  than  the  "classic"
  289.         which has been used to this point.  As you read articles  on
  290.         C, you will see programs written in the "classic" style,  so
  291.         you need to be capable of reading them.  It would be  highly
  292.         recommended,  however, that you learn and use  the  "modern"
  293.         method  which will be covered shortly in this tutorial.   We
  294.         will  return to this program and show how to fix it so  that
  295.         it will work in spite of the bug.
  296.  
  297.                              SCOPE OF VARIABLES
  298.  
  299.              Load the next program,  SCOPE.C,  and display it for  a
  300.         discussion  of the scope of variables in a program.   Ignore
  301.         the 4 statements in lines 2 through 5 of this program for  a
  302.         few moments, and we will discuss them later.
  303.  
  304.              The first variable defined is a global variable "count"
  305.         which  is available to any function in the program since  it
  306.         is defined before any of the functions.   In addition, it is
  307.         always  available  because  it does not come and go  as  the
  308.         program  is  executed.   (That  will  make  sense  shortly.)
  309.         Farther down in the program,  another global variable  named
  310.         "counter"  is  defined  which  is also  global  but  is  not
  311.         available  to the main program since it is defined following
  312.         the main program.  A global variable is any variable that is
  313.         defined  outside of any function.   Note that both of  these
  314.         variables  are sometimes referred to as  external  variables
  315.         because they are external to any functions.
  316.  
  317.              Return  to  the  main  program and  you  will  see  the
  318.         variable  "index"  defined as an integer.   Ignore the  word
  319.         "register" for the moment.   This variable is only available
  320.  
  321.  
  322.                                   Page 33
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.             Chapter 5 - Functions, variables, and prototypes
  333.  
  334.  
  335.         within the main program because that is where it is defined.
  336.         In addition, it is an "automatic" variable, which means that
  337.         it  only comes into existence when the function in which  it
  338.         is  contained  is  invoked,  and ceases to  exist  when  the
  339.         function  is  finished.   This  really  means  nothing  here
  340.         because the main program is always in operation,  even  when
  341.         it  gives  control to another function.  Another integer  is
  342.         defined  within  the  "for"  braces,  namely  "stuff".   Any
  343.         pairing  of braces can contain a variable  definition  which
  344.         will  be  valid  and  available only while  the  program  is
  345.         executing statements within those braces.  The variable will
  346.         be  an  "automatic" variable and will cease  to  exist  when
  347.         execution leaves the braces.   This is convenient to use for
  348.         a loop counter or some other very localized variable.
  349.  
  350.                        MORE ON "AUTOMATIC" VARIABLES
  351.  
  352.              Observe  the  function named "head1" in line  26  which
  353.         looks  a  little funny because of "void" being  used  twice.
  354.         The purpose of the uses of the word "void" will be explained
  355.         shortly.  The  function contains a variable  named  "index",
  356.         which  has  nothing  to  do with the  "index"  of  the  main
  357.         program, except that both are automatic variables.  When the
  358.         program  is  not  actually  executing  statements  in   this
  359.         function,  this variable named "index" does not even  exist.
  360.         When "head1" is called, the variable is generated, and  when
  361.         "head1"  completes its task, the variable in  "head1"  named
  362.         "index"  is eliminated completely from existence.   Keep  in
  363.         mind  however that this does not affect the variable of  the
  364.         same  name  in the main program, since it  is  a  completely
  365.         separate entity.
  366.  
  367.              Automatic   variables  therefore,   are   automatically
  368.         generated and disposed of when needed.   The important thing
  369.         to remember is that from one call to a function to the  next
  370.         call,  the  value of an automatic variable is not  preserved
  371.         and must therefore be reinitialized.
  372.  
  373.                          WHAT ARE STATIC VARIABLES?
  374.  
  375.              An  additional variable type must be mentioned at  this
  376.         point,  the "static" variable.  By putting the reserved word
  377.         "static"  in  front  of  a  variable  declaration  within  a
  378.         function,  the variable or variables in that declaration are
  379.         static  variables  and will stay in existence from  call  to
  380.         call  of  the  particular function.
  381.  
  382.              By  putting  the  same reserved word  in  front  of  an
  383.         external variable, one outside of any function, it makes the
  384.         variable  private  and  not accessible to use in  any  other
  385.         file.  This implies that it is possible to refer to external
  386.  
  387.  
  388.                                   Page 34
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.             Chapter 5 - Functions, variables, and prototypes
  399.  
  400.  
  401.         variables  in other separately compiled files,  and that  is
  402.         true.  Examples of this usage will be given in chapter 14 of
  403.         this tutorial.
  404.  
  405.                          USING THE SAME NAME AGAIN
  406.  
  407.              Refer  to  the  function named  "head2".   It  contains
  408.         another  definition  of the variable  named  "count".   Even
  409.         though  "count"  has  already  been  defined  as  a   global
  410.         variable,  it  is  perfectly all right to reuse the name  in
  411.         this  function.   It is a completely new variable  that  has
  412.         nothing to do with the global variable of the same name, and
  413.         causes  the  global  variable  to  be  unavailable  in  this
  414.         function.   This allows you to write programs using existing
  415.         functions  without worrying about what names were  used  for
  416.         variables in the functions because there can be no conflict.
  417.         You  only  need to worry about the variables that  interface
  418.         with the functions.
  419.  
  420.                         WHAT IS A REGISTER VARIABLE?
  421.  
  422.              Now  to  fulfill  a promise made earlier about  what  a
  423.         register  variable  is.   A  computer can  keep  data  in  a
  424.         register  or  in  memory.   A register  is  much  faster  in
  425.         operation  than  memory  but there are  very  few  registers
  426.         available  for the programmer to use.  If there are  certain
  427.         variables  that are used extensively in a program,  you  can
  428.         designate  that  those  variables  are to  be  stored  in  a
  429.         register  if possible in order to speed up the execution  of
  430.         the  program.  Your compiler probably allows you to use  one
  431.         or  more  register  variables  and  will  ignore  additional
  432.         requests  if  you  request more  than  are  available.   The
  433.         documentation for your compiler will list how many registers
  434.         are  available with your compiler.  It will also inform  you
  435.         of what types of variables can be stored in a register.
  436.  
  437.                         WHERE DO I DEFINE VARIABLES?
  438.  
  439.              Now for a refinement on a general rule stated  earlier.
  440.         When  you have variables brought to a function as  arguments
  441.         to  the function, and you are using the "classic"  style  of
  442.         programming, they are defined immediately after the function
  443.         name and prior to the opening brace for the program.   Other
  444.         variables used in the function are defined at the  beginning
  445.         of the function, immediately following the opening brace  of
  446.         the function, and before any executable statements.
  447.  
  448.                             WHAT IS PROTOTYPING?
  449.  
  450.              A  prototype  is a "model" of the real thing  and  when
  451.         programming with a good up-to-date C compiler, you have  the
  452.  
  453.  
  454.                                   Page 35
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.             Chapter 5 - Functions, variables, and prototypes
  465.  
  466.  
  467.         ability  to  define  a  "model" of  each  function  for  the
  468.         compiler.   The compiler can then use the "model"  to  check
  469.         each of your calls to the function and determine if you have
  470.         used  the correct number of arguments in the  function  call
  471.         and  if they are of the correct type.  By using  prototypes,
  472.         you  let the compiler do some additional error checking  for
  473.         you.   The  ANSI standard for C should be released  late  in
  474.         1987,   and  will  contain  prototyping  as  part   of   its
  475.         recommended  standard.   Every  good C  compiler  will  have
  476.         prototyping available, so you should learn to use it.
  477.  
  478.              Returning to lines 3, 4, and 5 in SCOPE.C, we have  the
  479.         prototypes  for  the three functions  contained  within  the
  480.         program.   The first "void" in each line tells the  compiler
  481.         that  these particular functions do not return a  value,  so
  482.         that the compiler would flag the statement index =  head1();
  483.         as  an  error because nothing is returned to assign  to  the
  484.         variable  "index".  The word "void" within  the  parentheses
  485.         tells the compiler that this function requires no parameters
  486.         and  if a variable were included, it would be an  error  and
  487.         the  compiler would issue a warning message.  If  you  wrote
  488.         the  statement  head1(index);, it would be  a  error.   This
  489.         allows  you  to use type checking when programming in  C  in
  490.         much the same manner that it is used in Pascal, Modula 2, or
  491.         Ada.
  492.  
  493.              You should enable prototype checking with your compiler
  494.         at this time, if it is available with your compiler.
  495.  
  496.              Line 2 of SCOPE.C tells the system to go to the include
  497.         files  and  get the file named STDIO.H  which  contains  the
  498.         prototypes  for the standard input and output  functions  so
  499.         they can be checked for proper variable types.  Don't  worry
  500.         about the "include" yet, it will be covered in detail  later
  501.         in this tutorial.
  502.  
  503.              Compile  and run this program.
  504.  
  505.                          STANDARD FUNCTION LIBRARIES
  506.  
  507.              Every  compiler  comes  with some  standard  predefined
  508.         functions  which  are available for  your  use.   These  are
  509.         mostly   input/output   functions,   character  and   string
  510.         manipulation  functions, and math functions.  We will  cover
  511.         most  of  these  in  subsequent  chapters.   Prototypes  are
  512.         defined  for you by the writer of your compiler for  all  of
  513.         the  functions that are included with your compiler.  A  few
  514.         minutes spent studying your reference Guide will give you an
  515.         insight in where the prototypes are defined for each of  the
  516.         functions.
  517.  
  518.  
  519.  
  520.                                   Page 36
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.             Chapter 5 - Functions, variables, and prototypes
  531.  
  532.  
  533.              In addition,  most compilers have additional  functions
  534.         predefined that are not standard but allow the programmer to
  535.         get the most out of his particular computer.  In the case of
  536.         the  IBM-PC and compatibles,  most of these functions  allow
  537.         the  programmer  to use the BIOS services available  in  the
  538.         operating system, or to write directly to the video  monitor
  539.         or to any place in memory.  These will not be covered in any
  540.         detail as you will be able to study these unique aspects  of
  541.         your compiler on your own.  Many of these kinds of functions
  542.         are used in the example programs in chapter 14.
  543.  
  544.  
  545.                              WHAT IS RECURSION?
  546.  
  547.              Recursion  is another of those  programming  techniques
  548.         that  seem very intimidating the first time you come  across
  549.         it,  but  if  you will load and display the example  program
  550.         named RECURSON.C, we will take all of the mystery out of it.
  551.         This  is probably the simplest recursive program that it  is
  552.         possible  to write and it is therefore a stupid  program  in
  553.         actual  practice,  but for purposes of illustration,  it  is
  554.         excellent.
  555.  
  556.              Recursion  is  nothing more than a function that  calls
  557.         itself.   It is therefore in a loop which must have a way of
  558.         terminating.   In the program on your monitor,  the variable
  559.         "index"  is  set to 8,  and is used as the argument  to  the
  560.         function  "count_dn".   The function simply  decrements  the
  561.         variable, prints it out in a message, and if the variable is
  562.         not zero, it calls itself, where it decrements the  variable
  563.         again, prints it, etc. etc. etc. Finally, the variable  will
  564.         reach  zero,  and the function will not call  itself  again.
  565.         Instead, it will return to the prior time it called  itself,
  566.         and  return again, until finally it will return to the  main
  567.         program and from there return to DOS.
  568.  
  569.              For  purposes  of understanding you can think of it  as
  570.         having 8 copies of the function "count_dn" available and  it
  571.         simply  called all of them one at a time,  keeping track  of
  572.         which  copy it was in at any given time.   That is not  what
  573.         actually  happened,  but it is a reasonable illustration for
  574.         you to begin understanding what it was really doing.
  575.  
  576.                               WHAT DID IT DO?
  577.  
  578.              A  better explanation of what actually happened  is  in
  579.         order.   When you called the function from itself, it stored
  580.         all  of the variables and all of the internal flags it needs
  581.         to  complete the function in a block  somewhere.   The  next
  582.         time it called itself,  it did the same thing,  creating and
  583.         storing  another  block of everything it needed to  complete
  584.  
  585.  
  586.                                   Page 37
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.             Chapter 5 - Functions, variables, and prototypes
  597.  
  598.  
  599.         that  function call.   It continued making these blocks  and
  600.         storing them away until it reached the last function when it
  601.         started  retrieving the blocks of data,  and using  them  to
  602.         complete  each function call.   The blocks were stored on an
  603.         internal part of the computer called the "stack".  This is a
  604.         part  of  memory carefully organized to store data  just  as
  605.         described above.  It is beyond the scope of this tutorial to
  606.         describe the stack in detail,  but it would be good for your
  607.         programming  experience to read some material describing the
  608.         stack.   A stack is used in nearly all modern computers  for
  609.         internal housekeeping chores.
  610.  
  611.              In using recursion,  you may desire to write a  program
  612.         with  indirect recursion as opposed to the direct  recursion
  613.         described  above.    Indirect  recursion  would  be  when  a
  614.         function  "A"  calls the function "B",  which in turn  calls
  615.         "A",  etc.   This is entirely permissible,  the system  will
  616.         take  care of putting the necessary things on the stack  and
  617.         retrieving  them when needed again.   There is no reason why
  618.         you  could not have three functions calling each other in  a
  619.         circle,  or four,  or five,  etc.   The C compiler will take
  620.         care of all of the details for you.
  621.  
  622.              The thing you must remember about recursion is that  at
  623.         some  point,  something  must  go to  zero,  or  reach  some
  624.         predefined  point to terminate the loop.   If not,  you will
  625.         have  an  infinite  loop,  and the stack will  fill  up  and
  626.         overflow,  giving  you  an error and  stopping  the  program
  627.         rather abruptly.
  628.  
  629.              Compile and run this program.  If you compile this with
  630.         prototype  checking  on  you will  find  several  "warnings"
  631.         issued  during compilation.  Study these for a few  minutes.
  632.         One of the suggested exercises at the end of this chapter is
  633.         to modify this program to eliminate the prototype warnings.
  634.  
  635.                         ANOTHER EXAMPLE OF RECURSION
  636.  
  637.              The  program  named  BACKWARD.C is another  example  of
  638.         recursion,  so load it and display it on your screen.   This
  639.         program  is  similar to the last one except that it  uses  a
  640.         character array.  Each successive call to the function named
  641.         "forward_and_backwards" causes one character of the  message
  642.         to  be printed.  Additionally, each time the function  ends,
  643.         one of the characters is printed again, this time  backwards
  644.         as the string of recursive function calls is retraced.
  645.  
  646.              This  program  uses  the "modern"  method  of  function
  647.         definition  and  includes full prototype  definitions.   The
  648.         "modern"  method of function definition moves the  types  of
  649.         the  variables into the parentheses along with the  variable
  650.  
  651.  
  652.                                   Page 38
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.  
  662.             Chapter 5 - Functions, variables, and prototypes
  663.  
  664.  
  665.         names  themselves.   The  final  result  is  that  the  line
  666.         containing   the   function  name  looks   more   like   the
  667.         corresponding line in Pascal, Modula 2, or Ada.
  668.  
  669.              Don't worry about the character array defined in line 9
  670.         or  the  other  new  material  presented  here.   After  you
  671.         complete chapter 7 of this tutorial,  this program will make
  672.         sense.   It  was  felt that introducing a second example  of
  673.         recursion was important so this file is included here.
  674.  
  675.              Compile  and run this program with  prototype  checking
  676.         enabled and observe the results.
  677.  
  678.                  HOW TO WORK AROUND THE TURBO C (v1.0) BUG
  679.  
  680.              If you are using Turbo C version 1.00, load and display
  681.         the  program named FLOATSQ2.C which is an exact copy of  the
  682.         program   FLOATSQ.C   which  we  considered   earlier   with
  683.         prototyping added.  The return of the erroneous zeros is now
  684.         repaired  and the correct values are returned.   Apparently,
  685.         when  the  coders of Turbo C at Borland  were  testing  this
  686.         compiler,  they used prototyping and never found  this  bug.
  687.         The  use  of  prototyping  is a  good  practice  for  all  C
  688.         programmers to get into.
  689.  
  690.              Several things should be mentioned about this  program.
  691.         First, the word "float" at the beginning of lines 27 and  35
  692.         indicate to the compiler that these functions are  functions
  693.         that return "float" type values.  Also, since the prototypes
  694.         are  given before "main", the functions are not required  to
  695.         be identified in line 12 as they were in line 7 of FLOATSQ.C
  696.         earlier  in this chapter.  They can be included in line  12,
  697.         but they are not required to be.
  698.  
  699.              Notice  also that the type of the variable  "inval"  is
  700.         included  within  the parentheses in line 27.  It  would  be
  701.         very educational for you to modify this program so that  you
  702.         included  a  call  to "sqr" with a variable  of  type  "int"
  703.         within  the  parentheses to see what kind of a  warning  you
  704.         would  get.   Do  the  same thing  in  the  program  without
  705.         prototype checking, FLOATSQ.C.
  706.  
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.                                   Page 39
  719.  
  720.  
  721.  
  722.  
  723.  
  724.  
  725.  
  726.  
  727.  
  728.             Chapter 5 - Functions, variables, and prototypes
  729.  
  730.  
  731.         PROGRAMMING EXERCISES
  732.  
  733.         1.   Rewrite  TEMPCONV.C,  from an earlier chapter, and move
  734.              the temperature calculation to a function.
  735.  
  736.         2.   Write a program that writes your name on the monitor 10
  737.              times by calling a function to do the writing. Move the
  738.              called function ahead of the "main" function to see  if
  739.              your C compiler will allow it.
  740.  
  741.         3.   Add  prototyping  to the program  named  RECURSON.C  to
  742.              eliminate the warnings.
  743.  
  744.  
  745.  
  746.  
  747.  
  748.  
  749.  
  750.  
  751.  
  752.  
  753.  
  754.  
  755.  
  756.  
  757.  
  758.  
  759.  
  760.  
  761.  
  762.  
  763.  
  764.  
  765.  
  766.  
  767.  
  768.  
  769.  
  770.  
  771.  
  772.  
  773.  
  774.  
  775.  
  776.  
  777.  
  778.  
  779.  
  780.  
  781.  
  782.  
  783.  
  784.                                   Page 40
  785.